cc.Class({
    extends: cc.Component,

    properties: {

        content: cc.Node,
        prefab: cc.Prefab,

        selfRankItemNode:cc.Node,
        viewList:cc.Node, //列表
        rankHead:cc.Node, //三个头像
        oneHead:cc.Node,  //一个头像
        rankOneHeadItem:cc.Node,

        rankHeadItemNode_1:cc.Node,
        rankHeadItemNode_2:cc.Node,
        rankHeadItemNode_3:cc.Node,

        itemList:[cc.Node],//条目列表
        dataList:[cc.Object],//数据列表


        nickName : "",
        gender   : "",
        avatarUrl: "",
        score    : 0,

        curKey:"score",//当前的key 目前两种玩法的key是：score score_2
    },

    start () {
        let _self = this;
        this.selfRankItem = this.selfRankItemNode.getComponent("rankItem");
        this.rankHeadItem = [];
        this.rankHeadItem.push(this.rankHeadItemNode_1.getComponent("rankItem"));
        this.rankHeadItem.push(this.rankHeadItemNode_2.getComponent("rankItem"));
        this.rankHeadItem.push(this.rankHeadItemNode_3.getComponent("rankItem"));
        //单独一个头像
        this.rankOneItem = this.rankOneHeadItem.getComponent("rankItem");

        wx.onMessage( data => {
            if(data.type == '0' || data.type == '2'){
                this.curKey = "score";
                if(data.type == '0'){ 
                    this.curKey = "score";
                }else if(data.type == '2'){
                    this.curKey = "score_2";
                }
                this.selfRankItemNode.active = false;
                this.viewList.active = true;
                this.rankHead.active = false;
                this.oneHead.active  = false;

                // var nickName = data.nickName;
                // var gender   = data.gender;
                // var avatarUrl= data.avatarUrl;
                // var score    = data.score;
                // this.nickName = nickName;
                // this.gender   = gender;
                // this.avatarUrl= avatarUrl;
                // this.score    = score;
                // if(nickName && avatarUrl){
                //     this.selfRankItem.setData(this.avatarUrl, this.nickName, this.score, -1);
                // }
                
                this.refresh();
            }else if(data.type == '1' || data.type == '3'){ //刷新
                this.curKey = "score";
                if(data.type == '1'){ 
                    this.curKey = "score";
                }else if(data.type == '3'){
                    this.curKey = "score_2";
                }
                this.selfRankItemNode.active = false;
                this.viewList.active = false;
                this.rankHead.active = true;
                this.oneHead.active  = false;
                this.refresh_2();
            }
            else if(data.type == 4){  //左上角 只显示距离最近的一个分数
                var nickName  = data.nickName;
                var gender    = data.gender;
                var avatarUrl = data.avatarUrl;
                var score     = data.score;
                this.nickName = nickName;
                this.gender   = gender;
                this.avatarUrl= avatarUrl;
                this.score    = score;
                console.log(data);

                this.curKey = "score_2";
                this.selfRankItemNode.active = false;
                this.viewList.active = false;
                this.rankHead.active = false;
                this.oneHead.active  = true;
                this.refresh_3();
            }
        });

        
    },
    refresh(){
        this.hideAllItem();//隐藏所有的条目
        let _self = this;
        wx.getFriendCloudStorage({
            keyList: [_self.curKey], // 你要获取的、托管在微信后台都key
            success: function (res) {
                if(!res.data || res.data.length == 0)
                {
                    console.log("getFriendCloudStorage success! " + res);
                    return;
                }
                _self.dataList = []; //清空
                for (let i = 0; i < res.data.length; i++) {
                    let friend = res.data[i];
                    // console.log("friend: " + friend);
                    var err = ' stop getting friends\' infos';
                    if (!_self.preSettingData(friend, err, i)) {
                        return;
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //排序
                for(var i = 0; i < _self.dataList.length; i++){
                    for(var j = i+1; j < _self.dataList.length; j++){
                        var a = parseInt(_self.dataList[i].score);
                        var b = parseInt(_self.dataList[j].score);
                        if(a < b){
                            var obj = _self.dataList[j];
                            _self.dataList[j] = _self.dataList[i];
                            _self.dataList[i] = obj;
                        }
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //修改UI
                for(var i = 0; i < _self.dataList.length; i++){
                    var obj = _self.dataList[i];
                    // console.log("修改UI: " + JSON.stringify(obj));
                    _self.showUserData(obj.nickName, obj.avatarUrl, obj.score + "", i);
                }
                var height = _self.dataList.length * 100;
                if(height < 600)
                    height = 600
                _self.content.height = height;
            },
            fail: function (res) {
                console.log("getFriendCloudStorage failed! " + res);
            }
        });
    },
    
    preSettingData (user, str, idx) {
        if (!user) {
            console.log(str);
            return false;
        }
        console.log("user:" + JSON.stringify(user));
        let nickName = user.nickname;
        let avatarUrl = user.avatarUrl;
        let KVDataList= user.KVDataList;
        let score = this.getValByList(KVDataList, this.curKey);
        // let level = this.getValByList(KVDataList, "level");
        // console.log("score: " + score);
        var obj = {
            "nickName" :nickName,
            "avatarUrl":avatarUrl,
            "score":score,
        };
        this.dataList.push(obj); //添加数据
        return true;
    },
    showUserData (nickName, avatarUrl, data, idx) {
        let node = this.getItemByIdx(idx);
        if(node == null){
            node = cc.instantiate(this.prefab);
            node.parent = this.content;
            this.itemList.push(node);
        }
        node.active = true;
        var item = node.getComponent("rankItem");
        item.setData(avatarUrl, nickName, data, idx+1);
        //刷新自己的排名
        // console.log(nickName + " " + this.nickName)
        if(nickName == this.nickName){
            this.selfRankItem.setData(this.avatarUrl, this.nickName, this.score, idx+1);
        }
        
 
    },
    getValByList(list, key){ //通过key取出数值
        for(var i = 0; i < list.length; i++){
            let obj = list[i];
            if(obj.key == key){
                let val = obj.value;
                return val;
            }
        }
        return "0";
    },
    getItemByIdx(idx){
        if(idx < 0 || idx >= this.itemList.length)
            return null;
        return this.itemList[idx];
    },
    hideAllItem(){
        for(var i = 0; i < this.itemList.length; i++){
            this.itemList[i].active = false;
        }
    },

    refresh_2(){
        // this.hideAllItem();//隐藏所有的条目
        let _self = this;
        wx.getFriendCloudStorage({
            keyList: [_self.curKey], // 你要获取的、托管在微信后台都key
            success: function (res) {
                if(!res.data || res.data.length == 0)
                {
                    console.log("getFriendCloudStorage success! " + res);
                    return;
                }
                _self.dataList = []; //清空
                for (let i = 0; i < res.data.length; i++) {
                    let friend = res.data[i];
                    // console.log("friend: " + friend);
                    var err = ' stop getting friends\' infos';
                    if (!_self.preSettingData(friend, err, i)) {
                        return;
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //排序
                for(var i = 0; i < _self.dataList.length; i++){
                    for(var j = i+1; j < _self.dataList.length; j++){
                        var a = parseInt(_self.dataList[i].score);
                        var b = parseInt(_self.dataList[j].score);
                        if(a < b){
                            var obj = _self.dataList[j];
                            _self.dataList[j] = _self.dataList[i];
                            _self.dataList[i] = obj;
                        }
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //修改UI
                for(var i = 0; i < _self.rankHeadItem.length; i++){
                    _self.rankHeadItem[i].node.active = false;
                }
                for(var i = 0; i < _self.dataList.length && i < 3; i++){
                    var obj = _self.dataList[i];
                    // console.log("修改UI: " + JSON.stringify(obj));
                    _self.rankHeadItem[i].node.active = true;
                    _self.rankHeadItem[i].setData(obj.avatarUrl, obj.nickName, obj.score, i+1);
                }
            },
            fail: function (res) {
                console.log("getFriendCloudStorage failed! " + res);
            }
        });
    },
    refresh_3(){
        // this.hideAllItem();//隐藏所有的条目
        let _self = this;
        wx.getFriendCloudStorage({
            keyList: [_self.curKey], // 你要获取的、托管在微信后台都key
            success: function (res) {
                if(!res.data || res.data.length == 0)
                {
                    console.log("getFriendCloudStorage success! " + res);
                    return;
                }
                _self.dataList = []; //清空
                for (let i = 0; i < res.data.length; i++) {
                    let friend = res.data[i];
                    // console.log("friend: " + friend);
                    var err = ' stop getting friends\' infos';
                    if (!_self.preSettingData(friend, err, i)) {
                        return;
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //排序
                for(var i = 0; i < _self.dataList.length; i++){
                    for(var j = i+1; j < _self.dataList.length; j++){
                        var a = parseInt(_self.dataList[i].score);
                        var b = parseInt(_self.dataList[j].score);
                        if(a < b){
                            var obj = _self.dataList[j];
                            _self.dataList[j] = _self.dataList[i];
                            _self.dataList[i] = obj;
                        }
                    }
                }
                // console.log("dataList len: " + _self.dataList.length);
                //修改UI
                var rank = -1;
                for(var i = _self.dataList.length-1; i >= 0; i--){
                    var obj = _self.dataList[i];
                    // _self.rankHeadItem[i].node.active = true;
                    // _self.rankHeadItem[i].setData(obj.avatarUrl, obj.nickName, obj.score, i+1);
                    console.log("" + obj.score + " xxx " + _self.score);
                    if(parseInt(obj.score) > parseInt(_self.score)){
                        _self.rankOneItem.setData(obj.avatarUrl, obj.nickName, obj.score, i+1);
                        rank = i;
                        break;
                    }
                }
                if(rank == -1){
                    _self.rankOneItem.setData(_self.avatarUrl, _self.nickName, _self.score, 1);
                }
            },
            fail: function (res) {
                console.log("getFriendCloudStorage failed! " + res);
            }
        });
    },
});
